package com.tjetc.service.impl;

import com.tjetc.mapper.UserMapper;
import com.tjetc.vo.FollowClinicInfoVo;
import com.tjetc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.mapper.FollowClinicMapper;
import com.tjetc.domain.FollowClinic;
import com.tjetc.service.FollowClinicService;

import java.util.List;

@Service
public class FollowClinicServiceImpl implements FollowClinicService{

    @Resource
    private FollowClinicMapper followClinicMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return followClinicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FollowClinic record) {
        return followClinicMapper.insert(record);
    }

    @Override
    public int insertSelective(FollowClinic record) {
        return followClinicMapper.insertSelective(record);
    }

    @Override
    public FollowClinic selectByPrimaryKey(Integer id) {
        return followClinicMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FollowClinic record) {
        return followClinicMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FollowClinic record) {
        return followClinicMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result isFollow(String phone, Integer cid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        FollowClinic followClinic = followClinicMapper.selectByUidAndHid(uid,cid);
        return Result.ok(followClinic != null);
    }
    @Override
    public Result follow(String phone, Integer cid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        Integer count = followClinicMapper.selectFollowCountByUid(uid);
        if (count >= 5){
            return Result.fail("关注数量不能超过五个");
        }
        FollowClinic oldFollowClinic = followClinicMapper.selectByUidAndHid(uid, cid);
        if (oldFollowClinic != null){
            return Result.fail("不能重复关注哦");
        }
        FollowClinic followClinic = FollowClinic.builder().cid(cid).uid(uid).build();
        int rows = followClinicMapper.insert(followClinic);
        return rows == 1?Result.ok():Result.fail("失败");
    }

    @Override
    public Result cancelFollow(String phone, Integer cid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        FollowClinic followClinic = followClinicMapper.selectByUidAndHid(uid, cid);
        if (followClinic == null){
            return Result.fail("失败");
        }
        int rows = followClinicMapper.deleteByPrimaryKey(followClinic.getId());
        return rows == 1 ? Result.ok() : Result.fail("失败");
    }
    @Override
    public Result selectFollowClinicInfo(Integer uid) {
        List<FollowClinicInfoVo> list = followClinicMapper.selectFollowClinicInfo(uid);
        return Result.ok(list);
    }
}
